﻿using System;
using System.Net;
using IBiz;

namespace Biz
{
    /// <summary>
    /// 信号灯
    /// </summary>
    public class Light : Ecmd
    {
        public Light(string relayIndex)
        {
            base.RelayIndex = int.Parse(relayIndex);
        }

        public override string GetValue()
        {
            string value;

            try
            {
                using (var client = new WebClient())
                {
                    var cmd = string.Format(DicGetCmd[base.RelayIndex / 8], base.Ip, base.RelayIndex % 9);
                    client.Proxy = null;
                    value = client.DownloadString(cmd);
                }
            }
            catch (Exception)
            {
                throw;
            }

            return value;
        }

        public override void Open()
        {
            try
            {
                using (var client = new WebClient())
                {
                    var cmd = string.Format(DicSetOnCmd[base.RelayIndex / 8], base.Ip, base.RelayIndex % 9);
                    client.Proxy = null;
                    client.DownloadString(cmd);
                    var voiceBroadcast = new VoiceBroadcast();
                    voiceBroadcast.playWarnWAVFile();
                }
            }
            catch (Exception)
            {

                throw;
            }

        }

        public override void Close()
        {
            try
            {
                using (var client = new WebClient())
                {
                    var cmd = string.Format(DicSetOffCmd[base.RelayIndex / 8], base.Ip, base.RelayIndex % 9);
                    client.Proxy = null;
                    client.DownloadString(cmd);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
